package com.example.carrierapp.viewmodel.itemmodel

import android.databinding.BaseObservable
import android.databinding.Bindable
import android.view.View
import com.android.databinding.library.baseAdapters.BR
import com.example.carrierapp.config.mStartActivity
import com.example.carrierapp.ui.main.dispatch.add.DispatchAddActivity
import com.example.carrierapp.util.ActivityUtil
import com.google.gson.annotations.SerializedName

class ItemDispatchModel : BaseObservable() {


    var shipmentNumber = "" // 发货编码

    var status = 0 // 状态= ['1', '2', '3']

    var statusName = "" // 发货计划状态名称

    var isSingle = false // 是否单次

    @SerializedName("isShort")
    var showShort = false // 是否短倒

    var productId = 0 // 产品Id

    var proName = "" // 货物名称

    var proPicPath = "" // 货物图片路径

    var lodingNumber = 0 // 装货编号

    var lodingName = "" // 装货地市区

    var lodingDetail = "" // 装货详细地址

    var unloadNumber = 0 // 卸货编号

    var unloadName = "" // 卸货地市区

    var unloadDetail = "" // 卸货详细地址

    private var estimatedVolumes = 0f // 预计运量

    private var distance = 0f // 公里数

    private var settlementType = 0 // 结算方式= ['1', '2', '3']

    var settlementTypeName = "" // 发货计划结算方式名称

    var creationTime = "" // 发货计划创建时间

    private var topTime: String? = null // 置顶时间 承运宝排序使用

    var shipperId = 0 // 托运人ID

    var shipperName = "" // 托运人名称及货主

    var isCompleted = false // 该发货单运是是否已全部送达

    var id = 0

    //单次调度是否产生运单
    private var isGenerateWaybill = false

    @Bindable
    var conComfirm = true
        get() = when (status) {
            2 -> false
            else -> isGenerateWaybill || isCompleted
        }

    @Bindable
    var canDispatch = true
        get() = if (isGuide) true else when {
            status == 2 -> false
            showShort -> false
            isGenerateWaybill -> false
            else -> true
        }

    var displayText = ""
        get() = "$proName | " + when (settlementType) {
            3 -> "${estimatedVolumes}吨/${distance}公里"
            else -> "${estimatedVolumes}吨"
        }

    var isGuide = false
        set(value) {
            field = value
            notifyPropertyChanged(BR.canDispatch)
        }
    var top = false
        get() = topTime.isNullOrEmpty().not()

    fun toDetails(view: View) {
        ActivityUtil.startDispatchDetail(view.context, id, status == 1)
    }

    fun toAdd(view: View) {
        view.context.mStartActivity(DispatchAddActivity::class.java,
                Pair("shipmentNumber", shipmentNumber),
                Pair("isSingle", isSingle),
                Pair("shipmentId", id))
    }

}